package com.hejia.alauda.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hejia.alauda.entity.Fund;
import com.hejia.alauda.entity.FundTrade;
import com.hejia.alauda.entity.FundTrade.ActiveType;
import com.hejia.alauda.entity.User;
import com.hejia.alauda.entity.UserFundInfo;
import com.hejia.alauda.entity.example.FundExample;
import com.hejia.alauda.entity.example.FundTradeExample;
import com.hejia.alauda.exception.BusinessException;
import com.hejia.alauda.exception.BusinessExceptionCode;
import com.hejia.alauda.persistence.mybatis.FundMapper;
import com.hejia.alauda.persistence.mybatis.FundTradeMapper;
import com.hejia.alauda.persistence.mybatis.UserFundInfoMapper;
import com.hejia.alauda.persistence.mybatis.UserMapper;
import com.hejia.alauda.utils.Pager;

/**
 * 
 * 爱基金接口
 *
 *
 * Created by showone on 2015 上午11:23:36.
 *
 * @Copyright: 2015 www.hejiajinrong.com All rights reserved.
 */
@Service
public class FundServiceImpl implements FundService {
	@Resource
	private UserFundInfoMapper ufiMapper;

	@Resource
	private FundTradeMapper ftMapper;

	@Resource
	private FundMapper fundmapper;

	@Resource
	private UserMapper userMapper;

	/**
	 * 将用户账号与基金账号进行关联
	 * 
	 * @param custId
	 *            基金账号
	 * @param token
	 *            token值
	 * @param phoneNum
	 *            手机号码
	 * @return
	 * @throws BusinessException
	 */
	public boolean relateToFund(Long userId, String custId, String token,
			String phoneNum) throws BusinessException {
		UserFundInfo ufi = ufiMapper.selectByPrimaryKey(userId);
		if (ufi != null) {
			throw new BusinessException(BusinessExceptionCode.EXCEPTION_10002);
		}
		User user = userMapper.selectByPrimaryKey(userId);

		ufi = new UserFundInfo();
		ufi.setId(userId);
		ufi.setCreateDate(new Date());
		ufi.setModifyDate(new Date());
		ufi.setCustId(custId);
		ufi.setToken(token);
		ufi.setPhoneNum(phoneNum);
		ufi.setThirdId(user.getExternal());
		return ufiMapper.insert(ufi) > 0;
	}

	/**
	 * 购买基金
	 * 
	 * @param userId
	 *            用户ID
	 * @param custId
	 *            基金账号
	 * @param fundCode
	 *            基金代码
	 * @param fundName
	 *            基金名称
	 * @param confirmedVol
	 *            购买金额
	 * @param acceptTime
	 *            申请时间
	 * @param confirmflagName
	 *            确认标志名称
	 * @param checkflagname
	 *            检查标志名称
	 * @param confirmTime
	 *            预计确认时间
	 * @return
	 * @throws BusinessException
	 */
	public boolean buy(Long userId, String custId, String fundCode,
			String fundName, String confirmedVol, String acceptTime,
			String confirmflagName, String checkflagname, String confirmTime)
			throws BusinessException {
		UserFundInfo ufi = ufiMapper.selectByPrimaryKey(userId);
		if (ufi == null) {
			throw new BusinessException(BusinessExceptionCode.EXCEPTION_10001);
		}
		/*
		 * 更新用户基金信息
		 */

		ufi.setLastTime(new Date());
		ufiMapper.updateByPrimaryKey(ufi);

		/*
		 * 插入基金交易记录
		 */
		FundTrade ft = new FundTrade();
		ft.setCreateDate(new Date());
		ft.setModifyDate(new Date());
		ft.setActiveType(FundTrade.ActiveType.BUY);
		ft.setCustId(custId);
		ft.setFundCode(fundCode);
		ft.setFundName(fundName);
		ft.setConfirmedVol(confirmedVol);
		ft.setAcceptTime(acceptTime);
		ft.setConfirmflagName(confirmflagName);
		ft.setCheckflagname(checkflagname);
		ft.setConfirmTime(confirmTime);
		return ftMapper.insert(ft) > 0;
	}

	/**
	 * 赎回基金
	 * 
	 * @param userId
	 *            用户ID
	 * @param custId
	 *            基金账号
	 * @param fundCode
	 *            基金代码
	 * @param fundName
	 *            基金名称
	 * @param confirmedVol
	 *            赎回份额
	 * @param acceptTime
	 *            申请时间
	 * @param confirmflagName
	 *            确认标志名称
	 * @param checkflagname
	 *            检查标志名称
	 * @param confirmTime
	 *            预计资金到账时间
	 * @return
	 * @throws BusinessException
	 */
	public boolean redemption(Long userId, String custId, String fundCode,
			String fundName, String confirmedVol, String acceptTime,
			String confirmflagName, String checkflagname, String confirmTime)
			throws BusinessException {
		UserFundInfo ufi = ufiMapper.selectByPrimaryKey(userId);
		if (ufi == null) {
			throw new BusinessException(BusinessExceptionCode.EXCEPTION_10001);
		}
		/*
		 * 更新用户基金信息
		 */

		ufi.setLastTime(new Date());
		ufiMapper.updateByPrimaryKey(ufi);

		/*
		 * 插入基金交易记录
		 */
		FundTrade ft = new FundTrade();
		ft.setCreateDate(new Date());
		ft.setModifyDate(new Date());
		ft.setActiveType(FundTrade.ActiveType.REDEMPTION);
		ft.setCustId(custId);
		ft.setFundCode(fundCode);
		ft.setFundName(fundName);
		ft.setConfirmedVol(confirmedVol);
		ft.setAcceptTime(acceptTime);
		ft.setConfirmflagName(confirmflagName);
		ft.setCheckflagname(checkflagname);
		ft.setConfirmTime(confirmTime);
		return ftMapper.insert(ft) > 0;
	}

	@Override
	public Pager<Fund> findAllFund(Pager<Fund> pager) {
		return findAllFund(pager, new FundExample());
	}

	@Override
	public Pager<Fund> findAllFund(Pager<Fund> pager, FundExample example) {
		if (pager == null) {
			pager = new Pager<Fund>();
			pager.setPageSize(Integer.MAX_VALUE);
		}
		if (example == null) {
			example = new FundExample();
		}
		example.setOrderByClause("id desc");
		pager.setList(fundmapper.selectByExample(example, pager));
		pager.setTotalCount(fundmapper.countByExample(example));
		return pager;
	}

	@Override
	public boolean saveFund(Fund fund) {
		fund.setCreateDate(new Date());
		return fundmapper.insert(fund) > 0;
	}

	@Override
	public Fund getFundById(Long id) {
		return fundmapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean modifyFund(Fund fund) {
		Fund fund2 = fundmapper.selectByPrimaryKey(fund.getId());
		fund2.setModifyDate(new Date());
		fund2.setFundName(fund.getFundName());
		fund2.setFundCode(fund.getFundCode());
		fund2.setReleaseTime(fund.getReleaseTime());
		fund2.setAccrual(fund.getAccrual());
		fund2.setFundManager(fund.getFundManager());
		fund2.setMinAmount(fund.getMinAmount());
		fund2.setOuttime(fund.getOuttime());
		fund2.setSaleCompany(fund.getSaleCompany());
		fund2.setScale(fund.getScale());
		fund2.setSalestate(fund.getSalestate());
		fund2.setType(fund.getType());
		fund2.setAnnualizedReturn(fund.getAnnualizedReturn());
		fund2.setActivitys(fund.getActivitys());
		fund2.setBuyCount(fund.getBuyCount());
		fund2.setCreateDay(fund.getCreateDay());
		return fundmapper.updateByPrimaryKey(fund2) > 0;
	}

	@Override
	public boolean deleteFund(Long id) {
		Fund fund = fundmapper.selectByPrimaryKey(id);
		return fundmapper.deleteByPrimaryKey(fund) > 0;
	}

	@Override
	public List<Fund> getFundList() {
		FundExample exp = new FundExample();
		Pager<Fund> pager = new Pager<Fund>();
		return fundmapper.selectByExample(exp, pager);
	}

	@Override
	public Fund getFundByFundcode(String fundcode) {
		FundExample exp = new FundExample();
		exp.appendCriterion("fundCode=", fundcode);
		return fundmapper.selectOneByExample(exp);
	}

	@Override
	public UserFundInfo getUserFundInfoByUserId(Long id) {

		return ufiMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean upFund(Long id) {
		Fund fund = fundmapper.selectByPrimaryKey(id);
		fund.setPublish(1);
		fund.setModifyDate(new Date());
		return fundmapper.updateByPrimaryKey(fund) > 0;
	}

	@Override
	public boolean downFund(Long id) {
		Fund fund = fundmapper.selectByPrimaryKey(id);
		fund.setPublish(0);
		fund.setModifyDate(new Date());
		return fundmapper.updateByPrimaryKey(fund) > 0;
	}

	@Override
	public boolean stopFund(Long id) {
		Fund fund = fundmapper.selectByPrimaryKey(id);
		fund.setPublish(2);
		fund.setModifyDate(new Date());
		return fundmapper.updateByPrimaryKey(fund) > 0;
	}

	@Override
	public int buyNumber(String fundcode) {
		FundTradeExample exp = new FundTradeExample();
		exp.appendCriterion("fundCode=", fundcode);
		exp.appendCriterion("activeType=", ActiveType.BUY);
		exp.appendCriterion("createDate>DATE(DATE_SUB(NOW(),INTERVAL 30 day))");

		return ftMapper.countByExample(exp);
	}
}
